from apps.course.models import Course
from apps.course.functions import course_reverse
from apps.participants.models import CourseParticipant
from apps.files.models import File

from django.contrib.auth.models import User

import datetime

def participants(request):
    TheCourse = Course.RequestCourse(request)
    if not TheCourse: return {}
    
    TheUser = request.user
    TheCourseParticipants = CourseParticipant.find(course=TheCourse) or []
    
    Contact_Dict = {'users':{}}
    for cp in TheCourseParticipants:
        cp.contact = course_reverse('email-user',TheCourse,kwargs={'username':cp.username()})
        cp.avatar = None
        files = File.objects.for_model(User).filter(object_pk=cp.user.id,filename__icontains="avatar.png")
        if files:
            cp.avatar = files[0].url()
        Contact_Dict[cp.user.username] = cp
        Contact_Dict['users'][cp.user.username] = cp
    return Contact_Dict